#!/bin/bash

source /home/shell/util.sh



# 安装函数
function install () {
    if [ ! -f "/opt/nodejs/bin/npm" ]; then
        bash /home/shell/nodejs-install.sh
    fi
    source /etc/profile.d/nodejs.sh
    npm install -g crontab-ui
    ln -s /opt/nodejs/bin/node /usr/bin/node
}

function config () {
    read -p "crontab-ui的端口[8000]：" ui_port
    ui_port=${ui_port:-"8000"}
    firewall-addport ${ui_port}
}


function run () {
bash /home/shell/serviced-install.sh<<EOF



crontabui
/opt/nodejs/bin/crontab-ui




EOF
    
    printInfo "修改服务配置..."
    sed -ri "s/Environment=/Environment=\"HOST=0.0.0.0\"\nEnvironment=\"PORT="${ui_port}"\"/g" /etc/systemd/system/crontabui.service
    systemctl daemon-reload
    systemctl restart crontabui.service

    printInfo "========================crontab-ui========================"
    printInfo "访问地址：http://${host_addr}:${ui_port}"
    printInfo "文档地址：https://hub.fastgit.org/alseambusher/crontab-ui"
    printInfo "========================crontab-ui========================"
}

install && config && run